home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / softwareproperties / MirrorTest.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.6 KB  |  174 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import threading
  5. import Queue
  6. import time
  7. import re
  8. import os
  9. import tempfile
  10. import aptsources
  11. from timeit import Timer
  12. import urllib
  13. import socket
  14. import random
  15. socket.setdefaulttimeout(2)
  16.  
  17. class MirrorTest(threading.Thread):
  18.     '''Determines the best mirrors by perfoming ping and download test.'''
  19.     
  20.     class PingWorker(threading.Thread):
  21.         """Use the command line command ping to determine the server's
  22.            response time. Using multiple threads allows to run several
  23.            test simultaneously."""
  24.         
  25.         def __init__(self, jobs, results, id, parent, borders = (0, 1), mod = (0, 0)):
  26.             self.borders = borders
  27.             self.mod = mod
  28.             self.parent = parent
  29.             self.id = id
  30.             self.jobs = jobs
  31.             self.results = results
  32.             self.match_result = re.compile('^rtt .* = [\\.\\d]+/([\\.\\d]+)/.*')
  33.             threading.Thread.__init__(self)
  34.  
  35.         
  36.         def run(self):
  37.             result = None
  38.             while MirrorTest.completed < MirrorTest.todo and self.parent.running.isSet():
  39.                 
  40.                 try:
  41.                     mirror = self.jobs.get(True, 1)
  42.                     host = mirror.hostname
  43.                 except:
  44.                     continue
  45.  
  46.                 self.parent.report_action('Pinging %s...' % host)
  47.                 commando = os.popen('ping -q -c 2 -W 1 -i 0.5 %s' % host, 'r')
  48.                 while True:
  49.                     line = commando.readline()
  50.                     if not line:
  51.                         break
  52.                     
  53.                     result = re.findall(self.match_result, line)
  54.                 MirrorTest.completed_lock.acquire()
  55.                 MirrorTest.completed += 1
  56.                 self.parent.report_progress(MirrorTest.completed, MirrorTest.todo, self.borders, self.mod)
  57.                 if result:
  58.                     self.results.append([
  59.                         float(result[0]),
  60.                         host,
  61.                         mirror])
  62.                 
  63.                 MirrorTest.completed_lock.release()
  64.  
  65.  
  66.     
  67.     def __init__(self, mirrors, test_file, running = None):
  68.         threading.Thread.__init__(self)
  69.         self.test_file = test_file
  70.         self.threads = []
  71.         MirrorTest.completed = 0
  72.         MirrorTest.completed_lock = threading.Lock()
  73.         MirrorTest.todo = len(mirrors)
  74.         self.mirrors = mirrors
  75.         if not running:
  76.             self.running = threading.Event()
  77.         else:
  78.             self.running = running
  79.  
  80.     
  81.     def run_full_test(self):
  82.         '''Run a test of the mirror test.'''
  83.         results_ping = self.run_ping_test(max = 10)
  84.         results = self.run_download_test(map((lambda r: r[2]), results_ping))
  85.         for t, h in results:
  86.             print h.hostname, t
  87.         
  88.  
  89.     
  90.     def report_action(self, text):
  91.         '''Should be used by all sub test to collect action status messages
  92.            in a central place.'''
  93.         print text
  94.  
  95.     
  96.     def report_progress(self, current, max, borders = (0, 100), mod = (0, 0)):
  97.         '''Should be used by all sub test to collect progress messages
  98.            in a central place.'''
  99.         print 'Completed %s of %s' % (current + mod[0], max + mod[1])
  100.  
  101.     
  102.     def run_ping_test(self, mirrors = None, max = None, borders = (0, 1), mod = (0, 0)):
  103.         '''Performs ping tests of the given mirrors and returns the
  104.            best results (specified by max).
  105.            Mod and borders could be used to tweak the reported result if
  106.            the download test is only a part of a whole series of tests.'''
  107.         if mirrors == None:
  108.             mirrors = self.mirrors
  109.         
  110.         jobs = Queue.Queue()
  111.         for m in mirrors:
  112.             jobs.put(m)
  113.         
  114.         results = []
  115.         for i in range(25):
  116.             t = MirrorTest.PingWorker(jobs, results, i, self, borders, mod)
  117.             self.threads.append(t)
  118.             t.start()
  119.         
  120.         for t in self.threads:
  121.             t.join()
  122.         
  123.         results.sort()
  124.         return results[0:max]
  125.  
  126.     
  127.     def run_download_test(self, mirrors = None, max = None, borders = (0, 1), mod = (0, 0)):
  128.         '''Performs download tests of the given mirrors and returns the
  129.            best results (specified by max).
  130.            Mod and borders could be used to tweak the reported result if
  131.            the download test is only a part of a whole series of tests.'''
  132.         
  133.         def test_download_speed(mirror):
  134.             url = '%s/%s' % (mirror.get_repo_urls()[0], self.test_file)
  135.             self.report_action('Downloading %s...' % url)
  136.             start = time.time()
  137.             
  138.             try:
  139.                 data = urllib.urlopen(url).read(102400)
  140.                 return time.time() - start
  141.             except:
  142.                 return 0
  143.  
  144.  
  145.         if mirrors == None:
  146.             mirrors = self.mirrors
  147.         
  148.         results = []
  149.         for m in mirrors:
  150.             if not self.running.isSet():
  151.                 break
  152.             
  153.             download_time = test_download_speed(m)
  154.             if download_time > 0:
  155.                 results.append([
  156.                     download_time,
  157.                     m])
  158.             
  159.             self.report_progress(mirrors.index(m), len(mirrors), (0.5, 1), mod)
  160.         
  161.         results.sort()
  162.         return results[0:max]
  163.  
  164.  
  165. if __name__ == '__main__':
  166.     distro = aptsources.distro.get_distro()
  167.     distro.get_sources(aptsources.SourcesList())
  168.     pipe = os.popen('dpkg --print-architecture')
  169.     arch = pipe.read().strip()
  170.     test_file = 'dists/%s/%s/binary-%s/Packages.gz' % (distro.source_template.name, distro.source_template.components[0].name, arch)
  171.     app = MirrorTest(distro.source_template.mirror_set.values(), test_file)
  172.     app.run_full_test()
  173.  
  174.